<template>
  <view class="container">
    <view class="form">
      <u-form ref="uForm" :model="registerForm">
        <u-form-item label="昵称" :label-width="130"
          ><u-input v-model="registerForm.nickName"
        /></u-form-item>
        <u-form-item label="性别" :label-width="130"
          ><u-input
            v-model="registerForm.sex"
            type="select"
            @click="registerForm.sexSelectShow = true"
          />
          <u-action-sheet
            v-model="registerForm.sexSelectShow"
            :list="registerForm.actionSheetList"
            @click="actionSheetCallback"
          ></u-action-sheet>
        </u-form-item>
        <u-form-item label="用户名" :label-width="130"
          ><u-input v-model="registerForm.userName"
        /></u-form-item>
        <u-form-item label="密码" :label-width="130"
          ><u-input v-model="registerForm.password" type="password"
        /></u-form-item>
        <u-form-item label="确认密码" :label-width="130"
          ><u-input v-model="registerForm.confirmPassword" type="password"
        /></u-form-item>
        <u-form-item label="手机号" :label-width="130"
          ><u-input v-model="registerForm.phoneNum"
        /></u-form-item>
        <u-form-item label="邮箱" :label-width="130"
          ><u-input v-model="registerForm.email"
        /></u-form-item>
      </u-form>
    </view>

    <u-button type="primary" @click="commit()">确定</u-button>
  </view>
  <!-- <u-modal
    :model-value="alertShowIf"
    :content="modalErrorMsg"
    @confirm="confirm()"
  ></u-modal> -->
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { registerFn } from '@/utils/modules/user.js'

const registerForm = ref({
  avatarUrl: '',
  nickName: '',
  userName: '',
  password: '',
  confirmPassword: '',
  email: '',
  phoneNum: '',
  sex: '',
  sexList: ['nan', 'nv'],
  actionSheetList: [
    {
      text: '男',
    },
    {
      text: '女',
    },
    {
      text: '保密',
    },
  ],
  sexSelectShow: false,
})

function actionSheetCallback(index) {
  registerForm.value.sex = registerForm.value.actionSheetList[index].text
}

// const modalErrorMsg = ref<String>('')
function commit() {
  registerFn({
    user: {
      nickName: registerForm.value.nickName,
      userName: registerForm.value.userName,
      openId: uni.getStorageSync('openId'), // 假设这是你的 openId 字段
      password: registerForm.value.password,
      email: registerForm.value.email,
      phoneNum: registerForm.value.phoneNum,
      sex: registerForm.value.sex,
    },
  })
    .then((res) => {
      console.log(res.data, '------res.data')
      uni.reLaunch({
        url: '/pages/login/login',
      })
    })
    .catch((err) => {
      console.log(err)
    })
}
</script>

<style lang="scss">
.container {
  width: 650rpx;
  margin-left: 50rpx;
  margin-top: 50rpx;
  .form {
    margin-bottom: 50rpx;
  }
}
</style>
